﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class Supplier
    {
        #region 封装属性

        private int mSupplierNum;

        public int SupplierNum
        {
            get { return mSupplierNum; }
            set { mSupplierNum = value; }
        }

        private string mSupplierName;

        public string SupplierName
        {
            get { return mSupplierName; }
            set { mSupplierName = value; }
        }

        private string mSupplierCode;

        public string SupplierCode
        {
            get { return mSupplierCode; }
            set { mSupplierCode = value; }
        }

        private string mAddress;

        public string Address
        {
            get { return mAddress; }
            set { mAddress = value; }
        }

        private string mContactPerson;

        public string ContactPerson
        {
            get { return mContactPerson; }
            set { mContactPerson = value; }
        }

        private string mTelephone;

        public string Telephone
        {
            get { return mTelephone; }
            set { mTelephone = value; }
        }

        private string mEmail;

        public string Email
        {
            get { return mEmail; }
            set { mEmail = value; }
        }

        private string mCreditLevel;

        public string CreditLevel
        {
            get { return mCreditLevel; }
            set { mCreditLevel = value; }
        }

        private int mFlagDisable;

        public int FlagDisable
        {
            get { return mFlagDisable; }
            set { mFlagDisable = value; }
        }

        #endregion

        private ISupplierDAL supplierDAL;

        public Supplier()
        {
            supplierDAL = Adapter.GetSupplier();
        }

        public void FetchObject(IDataReader dr)
        {
            if (dr!=null)
            {
                mSupplierNum = ConvertDataType.CheckFiledInt(dr["supplier_num"]);
                mSupplierName = ConvertDataType.CheckFiledString(dr["supplier_name"]);
                mSupplierCode = ConvertDataType.CheckFiledString(dr["supplier_code"]);
                mAddress = ConvertDataType.CheckFiledString(dr["address"]);
                mContactPerson = ConvertDataType.CheckFiledString(dr["contact_person"]);
                mTelephone = ConvertDataType.CheckFiledString(dr["telephone"]);
                mEmail = ConvertDataType.CheckFiledString(dr["email"]);
                mCreditLevel = ConvertDataType.CheckFiledString(dr["credit_level"]);
                mFlagDisable = ConvertDataType.CheckFiledInt(dr["flag_disable"]);
            }
        }

        public Supplier GetSupplier(string name)
        {
            Supplier obj = new Supplier();
            IDataReader dr = supplierDAL.Select(name);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }

        public DataTable GetAllSupplier()
        {
            return supplierDAL.Select();
        }

        public bool CreateSupplier(Supplier supplier)
        {

            return supplierDAL.Insert(supplier.mSupplierName, supplier.mSupplierCode, supplier.mAddress, supplier.mContactPerson, supplier.mTelephone, supplier.mEmail, supplier.mCreditLevel, supplier.mFlagDisable) > 0 ? true : false;

           // return supplierDAL.Insert( supplier.mSupplierName, supplier.mSupplierCode, supplier.mAddress, supplier.mContactPerson, supplier.mTelephone, supplier.mEmail, supplier.mCreditLevel, supplier.mFlagDisable) > 0 ? true : false;

        }

        public bool SetSupplier(Supplier supplier)
        {
            return supplierDAL.Update(supplier.mSupplierNum, supplier.mSupplierName, supplier.mSupplierCode, supplier.mAddress, supplier.mContactPerson, supplier.mTelephone, supplier.mEmail, supplier.mCreditLevel, supplier.mFlagDisable) > 0 ? true : false;
        }

        public bool RemoveSupplier(int supplierNum)
        {
            return supplierDAL.Delete(supplierNum) > 0 ? true : false;
        }
    }
}
